package org.dandsoft.blackbox.laboratory.samples.lighter.view;

import org.dandsoft.blackbox.laboratory.samples.lighter.World;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Created by IntelliJ IDEA.
 * User: dvilyuzhanin
 * Date: 23.03.2009 19:57:04
 */
public class ToolPanel extends JPanel {
    private World world;
    private WorldPanel panel;
    private Timer timer;
    private int repeatedCount;
    private int repeateCount;
    private LighterStatePanel statePanel;

    public ToolPanel(LighterStatePanel statePanel, WorldPanel worldPanel, World world) {
        setLayout(new FlowLayout());
        this.world = world;
        this.panel = worldPanel;
        this.statePanel = statePanel;

        JButton nextButton = new JButton("step");
        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ToolPanel.this.world.alive();
                ToolPanel.this.panel.repaint();
                ToolPanel.this.statePanel.update();
            }
        });

        JButton steps20Button = new JButton("20 steps");
        steps20Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (timer == null) {
                    repeateCount = 40;
                    repeatedCount = 0;
                    timer = new Timer(200, new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            ToolPanel.this.world.alive();
                            ToolPanel.this.panel.repaint();
                            ToolPanel.this.statePanel.update();
                            repeatedCount++;

                            if (repeatedCount > repeateCount) {
                                timer.stop();
                                timer = null;

                            }
                        }
                    });
                    timer.start();
                }

            }
        });

        final JButton runButton = new JButton("run");
        runButton.addActionListener(new ActionListener() {
            private JButton thisButton = runButton;

            public void actionPerformed(ActionEvent e) {
                if (timer == null) {
                    timer = new Timer(1, new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            ToolPanel.this.world.alive();
                            ToolPanel.this.panel.repaint();
                            ToolPanel.this.statePanel.update();
                            repeatedCount++;
                        }
                    });
                    timer.start();
                    thisButton.setText("stop");
                } else {
                    timer.stop();
                    timer = null;
                    thisButton.setText("run");
                }

            }
        });

        JButton setLighterPosition = new JButton("position");
        setLighterPosition.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ToolPanel.this.panel.waitSetLighterPosition();
            }
        });
        add(nextButton);
        add(steps20Button);
        add(runButton);
        add(setLighterPosition);
    }


}
